<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/2/28
 * Time: 19:58
 */

namespace Home\Controller;


use Common\Controller\HomeController;

class ListController extends HomeController
{
    public function __init(){
        $headConfig = [
            'css' => ['list.min'],
            'js' => ['goodList.min'],
            'title' => '商品列表',
        ];
        $this->assign('headConfig',$headConfig);
    }
    public function index(){
        $cid = I("get.cid",0,'intval');
        // 面包屑导航
        $fatherData = $this->getFather(M("Class")->select(),$cid);
        // 数组翻转
        $fatherData = array_reverse($fatherData);
        $this->assign('fatherData',$fatherData);
        // 递归找子级
        $cids = $this->getSon(M("Class")->select(),$cid);
        $cids[] = $cid;
        // 得到当前分类下的所有商品gid
        $gids = M("Goods")->where("cid in (" . implode(',',$cids) . ")")->getField("gid",true);
        // 判断当前分类下有无商品
        if($gids){
            $res = M("GoodsAttr")->alias("ga")->join("xm_tproperty tp on ga.tpid = tp.tpid")->where("ga.gid in (" . implode(',',$gids) . ")")->group("gattr")->select();
            // 按所属类型将结果分组
            $temp = [];
            foreach($res as $k => $v){
                $temp[$v['tpid']][] = $v;
            }
            foreach($temp as $k => $v){
                $finalTemp[] = [
                    'name' => M("Tproperty")->where("tpid=$k")->getField("tpname"),
                    'value' => $v,
                ];
            }
            $this->assign('finalTemp',$finalTemp);
            // 进行选择规格后的筛选处理
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) : array_fill(0,count($finalTemp),0);
            $this->assign("param",$param);
            foreach($param as $k => $v){
                if($v){
                    // 根据gaid找到gattr
                    $gattr = M("GoodsAttr")->where("gaid=$v")->getField("gattr");
                    $fiterGids[] = M("GoodsAttr")->where("gattr='{$gattr}'")->getField("gid",true);
                }
            }
            if($fiterGids){
                // 若在该条件下有商品
                $finalGids = $fiterGids[0];
                foreach($fiterGids as $k => $v){
                    // 求第一个结果数组和之后的每一个数组的交集
                    $finalGids = array_intersect($finalGids,$v);
                }
                // 将求到的结果和$gids求交集
                $finalGids = array_intersect($finalGids,$gids);
                if($finalGids){
                    $goodsData = M("Goods")->where("gid in (" . implode(',',$finalGids) . ")")->select();
                    $this->assign("goodsData",$goodsData);
                }else{
                    // 该规格下没有商品
                }
            }else{
                $finalGids = $gids;
                $goodsData = M("Goods")->where("gid in (" . implode(',',$finalGids) . ")")->select();
                $this->assign("goodsData",$goodsData);
            }
        }else{
            // 当前分类下暂无商品
        }
        $this->display();
    }

    // 找父级
    public function getFather($data,$cid){
        static $temp = [];
        foreach($data as $k => $v){
            if($v['cid'] == $cid){
                $temp[] = $v;
                $this->getFather($data,$v['pid']);
            }
        }
        return $temp;
    }

    // 找子级
    public function getSon($data,$cid){
        static $temp = [];
        foreach($data as $k => $v){
            if($v['pid'] == $cid){
                $temp[] = $v['cid'];
                $this->getSon($data,$v['cid']);
            }
        }
        return $temp;
    }
}